<?php
namespace src\logic;
use \classlib\core as C;
class lcharms {
	static $lcharms = 10;
	static $airtime = 600;
	/**************************************************
	 * 'charms'   => 会员当前的魅力值 'airtime'=> 会员倒计时时间
	 * 'lcharms'  => 会员的上限限制    'dcharms'=> 当天已经领取的数量
	 * 'dtoday'   => 当天累计的日期Ymd 'ccharms' => 消费的星币数
	 **************************************************/
	/**
	 * 同步信息到redis 魅力星
	 * @param int   $userid
	 * @param array $data 
	 */
	public static function set($userid, $data) {
		$redis = \classlib\cache::inst('redis')->inst();
		$ckey  = self::ckey($userid);
		$redis->hMset($ckey, $data);
	}
	
	/**
	 * 获取缓存中的共享key资料
	 * @param unknown $userid
	 * @return string
	 */
	private static function ckey($userid) {
		return 'charms_'.$userid;
	}
	
	/**
	 * 从 redis 获取魅力星
	 * @param  int   $userid
	 * @return array $data
	 */
	public static function get($userid) {
		$redis = \classlib\cache::inst('redis')->inst();
		$ckey  = self::ckey($userid);
		$data  = $redis->hGetAll($ckey);
		$uorm  = C\model::load('live.vs.userinfo')->set_dev($userid);
		$user  = $uorm->get_one($userid);
		/* if ($user['airtime'] < 1 || $user['lcharms'] < 1) {
			$uorm->save($userid, array('airtime'=>self::$airtime, 'lcharms'=>self::$lcharms));
		} */
		if (empty($data) || intval($data['airtime']) == 0) {
			$today   = intval(C\config::$nowtime/24/3600);
			$aritime = ($user['airtime'] < 1)? self::$airtime:$user['airtime'];
			$data    = array('charms'=>$user['charms'], 'airtime'=>$aritime, 'dcharms'=>0, 
	 			'lcharms'=>$user['lcharms'], 'ccharms'=>$user['ccharms'], 'dtoday'=>$today);
			$redis->hMset($ckey, $data);
		}
		return $data;
	}
}